<template>
    <div style="width:100%;height:100%;">
        <div style="width:100%;height:100%;" id="firstCharts"/>
    </div>
</template>
<script>
    import echarts from 'echarts';
    import mixins from '../minxis';
    export default {
        props: {
            chartData: {
                type: Object,
                default () {
                    return {};
                }
            }
        },
        mixins: [mixins],
        data () {
            return {
                example: null
            };
        },
        watch: {
            chartData: {
                handler (newVal) {
                    if (Object.keys(newVal).length !== 0) {
                        this.init();
                    }
                },
                deep: true,
                immediate: true
            }
        },
        methods: {
            init () {
                this.example = echarts.init(document.getElementById('firstCharts'));
                let option = {
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross',
                            crossStyle: {
                                color: '#999'
                            }
                        }
                    },
                    color: [ '#1f9cf0', '#ff7200' ],
                    toolbox: {
                        feature: {
                            dataView: {show: false, readOnly: false},
                            magicType: {show: false, type: ['line', 'bar']},
                            restore: {show: false},
                            saveAsImage: {show: false}
                        }
                    },
                    legend: {
                        data: ['当前密度', '当前卷长'],
                        textStyle: {
                            color: '#ced0d6'
                        },
                        orient: 'horizontal',
                        x: 'center',
                        y: 'top'
                    },
                    grid: {
                        containLabel: true,
                        top: '12%',
                        bottom: '4%',
                        left: 0,
                        right: 0
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: this.chartData.d1.map(item => item.time),
                            axisPointer: {
                                type: 'shadow'
                            },
                            axisLabel: {
                                textStyle: {
                                    color: '#ced0d6'
                                }
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            name: '',
                            min: 0,
                            // interval: 50,
                            axisLabel: {
                                formatter: '{value}',
                                textStyle: {
                                    color: '#ced0d6'
                                }
                            },
                            splitLine: {
                                show: true,
                                lineStyle: {
                                    color: '#39415b',
                                    type: 'solid'
                                }
                            }
                        },
                        {
                            type: 'value',
                            name: '',
                            min: 0,
                            // max: 25,
                            // interval: 50,
                            axisLabel: {
                                formatter: '{value}',
                                textStyle: {
                                    color: '#ced0d6'
                                }
                            },
                            axisLine: {
                                color: 'blur',
                                lineStyle: {
                                    type: 'solid',
                                    color: '#0d1733'
                                }
                            },
                            splitLine: {
                                show: true,
                                lineStyle: {
                                    color: '#3a435c',
                                    type: 'solid'
                                }
                            }
                        }
                    ],
                    series: [
                        {
                            name: '当前密度',
                            type: 'bar',
                            data: this.chartData.d1.map(item => item.density)
                        },
                        {
                            name: '当前卷长',
                            type: 'line',
                            yAxisIndex: 1,
                            data: this.chartData.d1.map(item => item.length)
                        }
                    ]
                };
                this.example.setOption(option);
                addEventListener('resize', () => {
                    this.example.resize();
                });
            }
        }
    };
</script>
